import { CHANGE_INPUT, ADD_ITEM, DELETE_ITEM, GET_LIST } from './actionTypes'

const defaultState = {
  placeholder : 'Write Something999',
  inputValue : '',
  routelist : [
    {uid:123,title:'跳转到骁骑兵并传值123'},
    {uid:456,title:'跳转到骁骑兵并传值456'},
    {uid:789,title:'跳转到骁骑兵并传值789'}
  ],
  list:[
        '早上1点，打游戏',
        '中午2点洗澡澡'
    ]
}  //默认数据

const reduceraction = (state = defaultState, action) => {
  // console.log(state,action)
  if(action.type === CHANGE_INPUT){
    let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
    newState.inputValue = action.value
    return newState      //这里 返回newstate给store，store会拿newstate去替换state
  }
  //state值只能传递，不能使用  reducer里只能接受state，不能改变state
  if(action.type === ADD_ITEM ){ //根据type值，编写业务逻辑
    // console.log('18state', state)
    let newState = JSON.parse(JSON.stringify(state)) 
    newState.list.push(newState.inputValue)  //push新的内容到列表中去
    newState.inputValue = ''
    // console.log('22newState', newState)
    return newState
  }
  if(action.type === DELETE_ITEM ){
    let newState = JSON.parse(JSON.stringify(state)) 
    newState.list.splice(action.index,1)  //删除数组中对应的值
    return newState
  }
  if(action.type === GET_LIST ){
    let newState = JSON.parse(JSON.stringify(state)) 
    newState.list = action.data.list //复制新的List数组进去
    return newState
  }

  return state
}

export default reduceraction

// export default (state = defaultState, action) => {
//   // console.log(state,action)
//   if(action.type === CHANGE_INPUT){
//     let newState = JSON.parse(JSON.stringify(state)) //深度拷贝state
//     newState.inputValue = action.value
//     return newState      //这里 返回newstate给store，store会拿newstate去替换state
//   }
//   //state值只能传递，不能使用  reducer里只能接受state，不能改变state
//   if(action.type === ADD_ITEM ){ //根据type值，编写业务逻辑
//     // console.log('18state', state)
//     let newState = JSON.parse(JSON.stringify(state)) 
//     newState.list.push(newState.inputValue)  //push新的内容到列表中去
//     newState.inputValue = ''
//     // console.log('22newState', newState)
//     return newState
//   }
//   if(action.type === DELETE_ITEM ){ 
//     let newState = JSON.parse(JSON.stringify(state)) 
//     newState.list.splice(action.index,1)  //删除数组中对应的值
//     return newState
//   }
//   return state
// }
// export default defaultState
